package com.yunxi.service.dubboService.impl.sto;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageInfo;
import com.yunxi.model.order.PmsVehicleStock;
import com.yunxi.model.sto.StoVehicleLendOrder;
import com.yunxi.model.sto.VehicleOffline;
import com.yunxi.service.localService.CodeCreateService;
import com.yunxi.service.localService.sto.StoVehicleLendOrderService;
import com.yunxi.service.localService.sto.VehicleOfflineService;
import com.yunxi.service.localService.sto.VehicleStockMService;
import com.yunxi.sto.pojo.StoVehicleLendOrderDetailVO;
import com.yunxi.sto.pojo.StoVehicleLendOrderQueryVO;
import com.yunxi.sto.pojo.StoVehicleLendOrderVO;
import com.yunxi.sto.pojo.VehicleStockFrozenVO;
import com.yunxi.sto.service.StoVehicleLendOrderDubboService;
import com.yunxi.sto.service.VehicleStockDubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import java.lang.reflect.InvocationTargetException;
import java.util.List;

/**
 * <p>
 * 借车单 服务实现类
 * </p>
 *
 * @author caoYong
 * @since 2019-05-30
 */
public class StoVehicleLendOrderDubboServiceImpl implements StoVehicleLendOrderDubboService {

    @Autowired
    @Qualifier(value = "stoVehicleLendOrderServiceImpl")
    StoVehicleLendOrderService stoVehicleLendOrderService;

    @Autowired
    @Qualifier(value = "codeCreateServiceImpl")
    CodeCreateService codeCreateService;

    @Autowired
    @Qualifier(value = "vehicleStockDubboServiceImpl")
    VehicleStockDubboService vehicleStockDubboService;
    
    @Autowired
    @Qualifier(value = "vehicleOfflineServiceImpl")
    VehicleOfflineService vehicleOfflineService;

    @Autowired
    @Qualifier(value = "vehicleStockMServiceImpl")
    VehicleStockMService vehicleStockMService;


    @Override
    public StoVehicleLendOrderVO getById(String id)throws Exception  {
        StoVehicleLendOrder stoVehicleLendOrder = stoVehicleLendOrderService.getById(id);
        if(stoVehicleLendOrder == null)throw new Exception("操作失败,无法根据主键:找到对应记录!");
        StoVehicleLendOrderVO stoVehicleLendOrderVO = new StoVehicleLendOrderVO();
        BeanUtils.copyProperties(stoVehicleLendOrder, stoVehicleLendOrderVO);
        return stoVehicleLendOrderVO;
    }

    /**
     * 借车单详情
     * @param id
     * @return
     * @throws Exception
     */
    @Override
    public StoVehicleLendOrderDetailVO loadDetail(String id) throws Exception {
        StoVehicleLendOrderDetailVO vo = stoVehicleLendOrderService.loadDetail(id);
        if(vo == null) throw new Exception("操作失败,无法根据主键:"+id+"找到借车详情!");
        return vo;
    }

    /**
     * 借车单维护
     * @param stoVehicleLendOrderVO vo
     * @throws InvocationTargetException e
     * @throws IllegalAccessException e
     */
    @Override
    public void saveOrUpdate(StoVehicleLendOrderVO stoVehicleLendOrderVO) throws Exception {
        Integer vehiclePurpose = stoVehicleLendOrderVO.getVehiclePurpose();//车辆用途
        StoVehicleLendOrder stoVehicleLendOrder = new StoVehicleLendOrder();
        BeanUtils.copyProperties(stoVehicleLendOrderVO, stoVehicleLendOrder);
        if(stoVehicleLendOrder.getLendOrderId() == null){
            stoVehicleLendOrder.setOrderState(1);//单据状态车辆未出库
            String slo = codeCreateService.createCode("SLO", 4);
            stoVehicleLendOrder.setLendOrderNo(slo);
        }
        else{
            throw new Exception("操作失败,借车单不可编辑");
        }
        //根据VIN码,更新车辆主数据冻结状态为借车锁定
        QueryWrapper<VehicleOffline> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.eq("VIN",stoVehicleLendOrder.getVin());
        VehicleOffline vehicleOffline2 = vehicleOfflineService.getOne(queryWrapper2);
        if(vehicleOffline2 != null){
            vehicleOffline2.setVehicleIsFrozen(1);
            vehicleOffline2.setIsLock(1);
            if(vehiclePurpose != null && vehiclePurpose.equals(1)){
                vehicleOffline2.setFrozenReasonKey(5);//冻结原因,内部借车
            }
            else if(vehiclePurpose != null && vehiclePurpose.equals(2)){
                vehicleOffline2.setFrozenReasonKey(8);//冻结原因,消耗领用
            }

            vehicleOfflineService.updateById(vehicleOffline2);

            QueryWrapper<PmsVehicleStock> queryWrapper3 = new QueryWrapper<>();
            queryWrapper3.eq("VIN",vehicleOffline2.getVin());
            PmsVehicleStock vehicleStock = vehicleStockMService.getOne(queryWrapper3);
            if(vehicleStock != null){
                vehicleStock.setVehicleIsFrozen("1");
                vehicleStockMService.saveOrUpdate(vehicleStock);
            }
        }
        stoVehicleLendOrderService.saveOrUpdate(stoVehicleLendOrder);

        //自动转出库计划
        stoVehicleLendOrderService.toInOutPlan(stoVehicleLendOrderVO,1);
        //判断是否需要转入库计划
        if(stoVehicleLendOrderVO.getIsNeedReturn().equals(1)){
            stoVehicleLendOrderService.toInOutPlan(stoVehicleLendOrderVO,0);
        }
    }

    @Override
    public void removeById(String id) {
        stoVehicleLendOrderService.removeById(id);
    }

    @Override
    public PageInfo<StoVehicleLendOrderQueryVO> findPageByVO(StoVehicleLendOrderQueryVO stoVehicleLendOrderVO, Integer pageNum, Integer pageSize) {
        PageInfo<StoVehicleLendOrderQueryVO> pageInfo = stoVehicleLendOrderService.pageByVo(stoVehicleLendOrderVO, pageNum, pageSize);
        return pageInfo;
    }

    @Override
    public List<StoVehicleLendOrderQueryVO> export(StoVehicleLendOrderQueryVO stoVehicleLendOrderQueryVO) throws Exception {
        return stoVehicleLendOrderService.export(stoVehicleLendOrderQueryVO);
    }

    @Override
    public void removeByListVo(List<StoVehicleLendOrderVO> stoVehicleLendOrderVOList) throws IllegalAccessException, InvocationTargetException, InstantiationException {
        stoVehicleLendOrderService.removeByListVo(stoVehicleLendOrderVOList);
    }

    @Override
    public void delAndReset(List<String> list, Integer isDel,String userId) throws Exception {
        stoVehicleLendOrderService.delAndReset(list,isDel,userId);
    }
}
